/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2021-12-11 17:21:33
 * @LastEditors: Clark
 * @LastEditTime: 2021-12-11 20:47:07
 * @Description: file content
 */

#include "xml_file.h"

using namespace tinyxml2;

XmlFile::XmlFile(std::string &fileName)
{
    doc = new tinyxml2::XMLDocument;
    this->fileName = fileName;
    doc->LoadFile(fileName.c_str());
}

bool XmlFile::init()
{
    if (nullptr == doc)
        return false;

    doc->LoadFile(fileName.c_str());
    return true;
}

bool XmlFile::add_user(User &user)
{
    XMLElement *element = doc->NewElement("user");
    doc->InsertEndChild(element);

    // 用户ID
    XMLElement* idElement = element->InsertNewChildElement("ID");
    idElement->SetText(user.ID);

    // 姓名
    XMLElement* accountElement = element->InsertNewChildElement("account");
    accountElement->SetText(user.account.c_str());

    // 密码
    XMLElement* passwdElement = element->InsertNewChildElement("passwd");
    passwdElement->SetText(user.passwd.c_str());

    printf("add success ID[%u] account[%s] passwd[%s]\n", user.ID, user.account.c_str(), user.passwd.c_str());
    return true;
}

bool XmlFile::delete_user(User &user)
{
    XMLElement *element = nullptr;
    XMLElement *deleteElement = nullptr;

    deleteElement = doc->FirstChildElement("user");

    while (deleteElement)
    {
        element = deleteElement->FirstChildElement("account");
        if (0 == strncmp(element->GetText(), user.account.c_str(), user.account.size()))
        {
            printf("delete account[%s] success\n", user.account.c_str());
            doc->DeleteNode(deleteElement);
            break;
        }

        deleteElement = deleteElement->NextSiblingElement();
    }

    return true;
}

bool XmlFile::select_user(User &user)
{
    XMLElement *element = nullptr;
    XMLElement *deleteElement = nullptr;

    deleteElement = doc->FirstChildElement("user");
    while (deleteElement)
    {
        element = deleteElement->FirstChildElement("account");
        if (0 == strncmp(element->GetText(), user.account.c_str(), user.account.size()))
        {
            element = deleteElement->FirstChildElement("ID");
            user.ID = atoi(element->GetText());

            element = deleteElement->FirstChildElement("passwd");
            user.passwd = (char *)element->GetText();

            printf("select success ID[%u] account[%s] passwd[%s]\n", user.ID, user.account.c_str(), user.passwd.c_str());
            break;
        }

        deleteElement = deleteElement->NextSiblingElement();
    }

    return true;
}

bool XmlFile::change_user(User &user)
{
    XMLElement *element = nullptr;
    XMLElement *deleteElement = nullptr;

    deleteElement = doc->FirstChildElement("user");
    while (deleteElement)
    {
        element = deleteElement->FirstChildElement("account");
        if (0 == strncmp(element->GetText(), user.account.c_str(), user.account.size()))
        {
            element = deleteElement->FirstChildElement("passwd");
            element->SetText(user.passwd.c_str());

            printf("change success ID[%u] account[%s] passwd[%s]\n", user.ID, user.account.c_str(), user.passwd.c_str());
            break;
        }

        deleteElement = deleteElement->NextSiblingElement();
    }

    return true;
}

unsigned int XmlFile::get_user_count()
{
    unsigned int count = 0;
    XMLElement *deleteElement = nullptr;

    deleteElement = doc->FirstChildElement("user");
    while (deleteElement)
    {
        count++;
        deleteElement = deleteElement->NextSiblingElement();
    }

    return count;
}

bool XmlFile::get_user_by_index(unsigned int index, User &user)
{
    XMLElement *deleteElement = nullptr;
    XMLElement *element = nullptr;

    deleteElement = doc->FirstChildElement("user");
    while (deleteElement && index > 0)
    {
        index--;
        if (0 == index)
        {
            element = deleteElement->FirstChildElement("ID");
            user.ID = atoi(element->GetText());

            element = deleteElement->FirstChildElement("account");
            user.account = (char *)element->GetText();

            element = deleteElement->FirstChildElement("passwd");
            user.passwd = (char *)element->GetText();

            printf("get user success ID[%u] account[%s] passwd[%s]\n", user.ID, user.account.c_str(), user.passwd.c_str());

            return true;
        }

        deleteElement = deleteElement->NextSiblingElement();
    }

    return false;
}

bool XmlFile::save_file()
{
    doc->SaveFile(fileName.c_str());
    return true;
}

XmlFile::~XmlFile()
{
    doc->SaveFile(fileName.c_str());
    delete doc;
}
